class SupplierController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max) params.max = 10
        [ supplierInstanceList: Supplier.list( sort:'id') ]
    }

    def show = {
        def supplierInstance = Supplier.get( params.id )

        if(!supplierInstance) {
            flash.message = "Supplier not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ supplierInstance : supplierInstance ] }
    }

    def delete = {
        def supplierInstance = Supplier.get( params.id )
        if(supplierInstance) {
            supplierInstance.delete()
            flash.message = "Supplier ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Supplier not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def supplierInstance = Supplier.get( params.id )

        if(!supplierInstance) {
            flash.message = "Supplier not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ supplierInstance : supplierInstance ]
        }
    }

    def update = {
        def supplierInstance = Supplier.get( params.id )
        if(supplierInstance) {
            supplierInstance.properties = params
            if(!supplierInstance.hasErrors() && supplierInstance.save()) {
                flash.message = "Supplier ${params.id} updated"
                redirect(action:show,id:supplierInstance.id)
            }
            else {
                render(view:'edit',model:[supplierInstance:supplierInstance])
            }
        }
        else {
            flash.message = "Supplier not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def supplierInstance = new Supplier()
        supplierInstance.properties = params
        return ['supplierInstance':supplierInstance]
    }

    def save = {
        def supplierInstance = new Supplier(params)
        if(!supplierInstance.hasErrors() && supplierInstance.save()) {
            flash.message = "Supplier ${supplierInstance.id} created"
            redirect(action:show,id:supplierInstance.id)
        }
        else {
            render(view:'create',model:[supplierInstance:supplierInstance])
        }
    }
}
